package com.panopset.io;

import java.io.File;
import java.io.StringWriter;

import com.panopset.compat.Strings;

public class AppProps {
	
	
	public AppProps(final String propFileName) {
		name = propFileName;
	}
	
	/**
	 * Properties.
	 */
	private FileProps fileProps;

	public FileProps getFileProps() {
		if (fileProps == null) {
			fileProps = new FileProps(new File(new Filez().combinePaths(
					getAppBase().getBaseDirectory(), this.getClass().getName()
							+ ".properties")));
		}
		return fileProps;
	}

	private AppBase ab;

	public AppBase getAppBase() {
		if (ab == null) {
			StringWriter sw = new StringWriter();
			sw.append(System.getProperty("user.home"));
			sw.append("/.");
			sw.append(getOrgHome());
			sw.append("/");
			sw.append(name.toLowerCase());
			sw.append("/");
			sw.append("conf");
			File appBaseDir = new File(sw.toString());
			ab = new AppBase(appBaseDir);
		}
		return ab;
	}
	

	/**
	 * 
	 * @param value
	 *            ie com.company
	 */
	public void setOrgHome(final String value) {
		orgHome = value;
	}

	/**
	 * Organization home directory.
	 */
	private String orgHome;

	/**
	 * @return Organization home directory.
	 */
	private String getOrgHome() {
		if (orgHome == null) {
			orgHome = System.getProperty("com.panopset.org.home");
			if (!(Strings.isPopulated(orgHome))) {
				orgHome = DFT_ORG_HOME;
			}
		}
		return orgHome;
	}

	private final String name;
	
	/**
	 * com.panopset.
	 */
	private static final String DFT_ORG_HOME = "com.panopset";


	public String get(final String key) {
		return getFileProps().get(key);
	}
	
	public void put(final String key, final String value) {
		getFileProps().put(key, value);
	}
}
